package com.maxlen.camunda.listener;

import com.maxlen.camunda.controller.WebSocketController;
import com.tcloudit.common.core.domain.R;
import com.tcloudit.system.api.RemoteThirdPartyService;
import com.tcloudit.system.api.RemoteUserService;
import com.tcloudit.system.api.domain.CamundaUserPost.EmailDetailTO;
import com.tcloudit.system.api.domain.CamundaUserPost.UserEmailTO;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyEndListener implements ExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(MyEndListener.class);

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteThirdPartyService remoteThirdPartyService;

    @Override
    public void notify(DelegateExecution execution) throws Exception {
        String businessKey = execution.getBusinessKey();
        System.out.println(businessKey);
        R<UserEmailTO> userNameEmailByUserId = remoteUserService.getUserNameEmailByUserId(businessKey);
        if (R.isSuccess(userNameEmailByUserId)) {
            UserEmailTO userEmailTO = userNameEmailByUserId.getData();
            WebSocketController.sendMessage(businessKey, "你的请假申请审批完成");

            if (userEmailTO.getUserName() != null && userEmailTO.getEmail() != null) {
//                EmailDetailTO emailDetailTO = new EmailDetailTO(userEmailTO.getUserName(), userEmailTO.getEmail(),
//                        "您的请假申请审核完毕" +
//                                "\n<a href=\"http://localhost/camunda/AFL\" type=\"primary\" target=\"_blank\">点击这里查看</a>");
//                R<String> stringR = remoteThirdPartyService.sendEmail(emailDetailTO);
//                if (R.isSuccess(stringR)) {
//                    logger.info("发送邮件给：{}", userEmailTO.getUserName());
//                    logger.info("邮件：{}\n", userEmailTO.getEmail());
//                } else {
//                    logger.error("发送邮件失败：{}\n", userEmailTO.getEmail());
//                }
            }
        }
    }
}
